package window

import (
	"syscall"
	"unsafe"

	"github.com/lxn/win"
)

// ScreenInfo 屏幕信息
type ScreenInfo struct {
	Index  int
	X      int
	Y      int
	Width  int
	Height int
}

// WindowManager 窗口管理器
type WindowManager struct {
	screens []ScreenInfo
}

// NewWindowManager 创建新的窗口管理器
func NewWindowManager() *WindowManager {
	wm := &WindowManager{}
	wm.initScreens()
	return wm
}

// initScreens 初始化屏幕信息
func (wm *WindowManager) initScreens() {
	// 获取主屏幕信息
	primaryScreen := wm.getScreenInfo(0)
	if primaryScreen != nil {
		wm.screens = append(wm.screens, *primaryScreen)
	}

	// 枚举所有显示器
	wm.enumDisplayMonitors()
}

// getScreenInfo 获取指定屏幕信息
func (wm *WindowManager) getScreenInfo(index int) *ScreenInfo {
	var info win.MONITORINFO
	info.CbSize = uint32(unsafe.Sizeof(info))

	// 获取主显示器句柄
	primaryMonitor := win.MonitorFromWindow(0, win.MONITOR_DEFAULTTOPRIMARY)
	if primaryMonitor == 0 {
		return nil
	}

	// 获取显示器信息
	if win.GetMonitorInfo(primaryMonitor, &info) {
		return &ScreenInfo{
			Index:  index,
			X:      int(info.RcMonitor.Left),
			Y:      int(info.RcMonitor.Top),
			Width:  int(info.RcMonitor.Right - info.RcMonitor.Left),
			Height: int(info.RcMonitor.Bottom - info.RcMonitor.Top),
		}
	}

	return nil
}

// enumDisplayMonitors 枚举所有显示器
func (wm *WindowManager) enumDisplayMonitors() {
	// 使用EnumDisplayMonitors枚举所有显示器
	user32 := syscall.NewLazyDLL("user32.dll")
	enumDisplayMonitors := user32.NewProc("EnumDisplayMonitors")
	enumDisplayMonitors.Call(0, 0, uintptr(syscall.NewCallback(wm.monitorEnumProc)), 0)
}

// monitorEnumProc 显示器枚举回调函数
func (wm *WindowManager) monitorEnumProc(hMonitor win.HMONITOR, hdcMonitor win.HDC, lprcMonitor *win.RECT, dwData uintptr) uintptr {
	var info win.MONITORINFO
	info.CbSize = uint32(unsafe.Sizeof(info))

	if win.GetMonitorInfo(hMonitor, &info) {
		screenInfo := ScreenInfo{
			Index:  len(wm.screens),
			X:      int(info.RcMonitor.Left),
			Y:      int(info.RcMonitor.Top),
			Width:  int(info.RcMonitor.Right - info.RcMonitor.Left),
			Height: int(info.RcMonitor.Bottom - info.RcMonitor.Top),
		}

		// 检查是否已存在相同的屏幕信息
		exists := false
		for _, existing := range wm.screens {
			if existing.X == screenInfo.X && existing.Y == screenInfo.Y &&
				existing.Width == screenInfo.Width && existing.Height == screenInfo.Height {
				exists = true
				break
			}
		}

		if !exists {
			wm.screens = append(wm.screens, screenInfo)
		}
	}

	return 1
}

// GetScreenInfo 获取指定索引的屏幕信息
func (wm *WindowManager) GetScreenInfo(index int) *ScreenInfo {
	if index >= 0 && index < len(wm.screens) {
		return &wm.screens[index]
	}
	return nil
}

// GetScreenIndex 根据坐标获取屏幕索引
func (wm *WindowManager) GetScreenIndex(x, y int) int {
	for i, screen := range wm.screens {
		if x >= screen.X && x < screen.X+screen.Width &&
			y >= screen.Y && y < screen.Y+screen.Height {
			return i
		}
	}

	// 如果没找到，返回主屏幕（索引0）
	if len(wm.screens) > 0 {
		return 0
	}
	return 0
}

// GetScreenCount 获取屏幕数量
func (wm *WindowManager) GetScreenCount() int {
	return len(wm.screens)
}

// GetAllScreens 获取所有屏幕信息
func (wm *WindowManager) GetAllScreens() []ScreenInfo {
	return wm.screens
}
